import time
from bs4 import BeautifulSoup
import requests
#下载
def down_from_url(url, dst):
    req = requests.get(url, stream=True)
    with open(dst, 'ab') as f:
        # 每次读取一个1024个字节
        for chunk in req.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
    return True
# 第一步：访问网页
# 第二步：找到详情页超链接
# 第三步：访问详情页，f12,找到video 的src链接
# 最后下载
url = 'https://bizhi.cheetahfun.com/dtag_109/'
response = requests.get(url)
response.encoding = "utf-8"
html_str = response.text
html =BeautifulSoup(html_str,"html.parser")
lias = html.find_all("a")
url_list = []
page_list = [url]
for lia in lias:
    if lia.attrs["href"].find(".shtml") != -1:
        url_list.append(lia.attrs["href"])
    if lia.attrs["href"].find("/dtag_109/index") != -1:
        page_list.append(lia.attrs["href"])

for url_info in url_list:
    response = requests.get(url_info)
    response.encoding = "utf-8"
    html_str = response.text
    html = BeautifulSoup(html_str,"html.parser")
    video = html.find("video")
    video_src = video.attrs["src"]
    file_name = "F:\\作业\\python\\爬虫图片\\"+str(time.time())+".mp4"
    down_from_url(video_src,file_name)
    print("成功下载")